package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



@Slf4j
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    @Autowired
    private OrderSettingService orderSettingService;

    /**
     * 根据日期修改每天可以预约的人数
     * @param orderSetting
     * @return
     * @throws ParseException
     */
    @PostMapping("/editNumberByDate")

    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = simpleDateFormat.format(orderSetting.getOrderDate());
       //根据日期更新更新预约的数量
        orderSettingService.updateOrderSetting(orderSetting.getNumber(), date);
        return new Result(true, MessageConst.ORDERSETTING_SUCCESS);
    }

    @PostMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile multipartFile) throws IOException {

        String originFileName = multipartFile.getOriginalFilename();
        log.info(originFileName);
        //如果为空，则上传失败
        if (StringUtils.isEmpty(originFileName)) {
            return new Result(true, MessageConst.IMPORT_ORDERSETTING_FAIL);
        }
        //只接收.xlxs的文件
        XSSFWorkbook workbook = new XSSFWorkbook(multipartFile.getInputStream());
        for (Sheet sheet : workbook) {
            //逐行读取，把读到的每行数据，封装到ordersetting中
            for (int i = 1; i < sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                OrderSetting orderSetting = new OrderSetting();
                Date date = row.getCell(0).getDateCellValue();
                double number = row.getCell(1).getNumericCellValue();
                orderSetting.setOrderDate(date);
                orderSetting.setNumber((int) number);
                //把封装好的数据存入到数据库
                orderSettingService.addOrderSetting(orderSetting);
            }
        }

        return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
    }

    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(Integer year, Integer month) {
        //按照年份和月份查询预约的数据
        List<OrderSetting> orderSettings = orderSettingService.selectOrderSettingByMonth(year, month);
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettings) {

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(orderSetting.getOrderDate());
            HashMap<String, Object> hashMap = new HashMap<>();
            //封装前端需要的格式
            hashMap.put("date", calendar.get(Calendar.DAY_OF_MONTH));
            hashMap.put("number", orderSetting.getNumber());
            hashMap.put("reservations", orderSetting.getReservations());
            //返回给前端多条数据
            mapList.add(hashMap);
        }
        return new Result(true, MessageConst.ORDERSETTING_SUCCESS, mapList);
    }

}
